import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// 展开收缩面板(更多、返回)
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'expansion_panel',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        splashColor: Colors.transparent,
      ),
      home: HomePage(),
    );
  }
}

class ExpansionPanelItem {
  final String headText;
  final Widget body;
  bool isExpanded;

  ExpansionPanelItem({this.headText, this.body, this.isExpanded});
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<ExpansionPanelItem> _expansionPanelItems;

  @override
  void initState() {
    super.initState();
    _expansionPanelItems = [
      ExpansionPanelItem(
          headText: 'Plan A',
          body: Container(
            padding: EdgeInsets.all(16.0),
            width: double.infinity,
            child: Text('Content for Plan A'),
          ),
          isExpanded: false),
      ExpansionPanelItem(
          headText: 'Plan B',
          body: Container(
            padding: EdgeInsets.all(16.0),
            width: double.infinity,
            child: Text('Content for Plan B'),
          ),
          isExpanded: false),
      ExpansionPanelItem(
          headText: 'Plan C',
          body: Container(
            padding: EdgeInsets.all(16.0),
            width: double.infinity,
            child: Text('Content for Plan C'),
          ),
          isExpanded: false),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('expansion_panel'),
      ),
      body: Center(
        child: Column(
          children: [
            ExpansionPanelList(
              // 当item展开收缩时回调,isExpanded当前状态
              expansionCallback: (int panelIndex, bool isExpanded) {
                print('$isExpanded');
                setState(() {
                  _expansionPanelItems[panelIndex].isExpanded = !isExpanded;
                });
              },
              children: _expansionPanelItems.map((item) {
                return ExpansionPanel(
                    headerBuilder: (ctx, isExpanded) {
                      return Container(
                        padding: EdgeInsets.all(16.0),
                        child: Text(
                          item.headText,
                          style: Theme.of(context).textTheme.headline4,
                        ),
                      );
                    },
                    body: item.body,
                    isExpanded: item.isExpanded);
              }).toList(),
            ),
          ],
        ),
      ),
    );
  }
}
